import store from '@/store/index.js'
var core = {
    url : function(route="",action='api'){
		route = route.replace('/','.');
		if (route.indexOf('http')==0 || route.indexOf('/index.php')>-1) return route;
		var baseurl = siteinfo.siteroot + '?i='+siteinfo.uniacid+'&c=entry&m=swa_education&do='+action+'&unifrom='+this.platform;
		if(action=='api'){
			baseurl = baseurl.replace('app/index.php','app/swaapi.php');
		}else if(this.platform=='h5' && baseurl.indexOf('http')!==0){
			baseurl = this.system.siteroot + baseurl;
		}
		if (route=='' || !route) return baseurl;
		return baseurl + '&r='+route;
	},
    // 处理page
    page : function(url='',data = {}){
		if(url=='') url = 'index/index';
		if(url=='#' || url.indexOf('javascript:')==0) return '';
		// ***#/pages/***
		if(url.indexOf('#/pages/')>0){
			let urls = url.split('#/pages/');
			url = urls[1];
		}else{
			// *** /#/pages/ ***
			url = url.replace('#/pages/','',url);			
		}
		if(url.indexOf('http')==0){
			return '/pages/index/web?url=' + encodeURIComponent(url);
		}
		if(url.indexOf('/pages/')!=0) url = '/pages/' + url;
		for (let key in data) {
			url += (url.indexOf('?') == -1 ? '?' : '&') + key + '=' + data[key]
		}
		return url;
	},
    evaljs:function(scripttext){
		let scripts = scripttext.split(":");
		let script = "core."+scripts[1]+"("+(typeof(scripts[2])!='undefined'?"'"+scripts[2]+"'":"")+")";
		eval(script);
	},
    // 统一跳转
	navito : function(page,data={},type = 0){
		if(page.indexOf('core:')==0) return this.evaljs(page);
		var url = this.page(page,data);
		if(url=='') return false;
		switch (type) {
			case 0:
				uni.navigateTo({
					url,
					fail: console.log
				})
				break;
			case 1:
				uni.redirectTo({
					url,
					fail: console.log
				})
				break;
			case 2:
				uni.reLaunch({
					url,
					fail: console.log
				})
				break;
			case 3:
				uni.navigateBack({
					delta: url,
					fail: console.log
				})
				break;
			case 4:
				uni.switchTab({
					url: url,
					fail: console.log
				})
				break;
		}
	},
	
}
module.exports = core;